﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace Publicuse.Entity.AttributeUtil.Validate
{
    /// <summary>
    /// 电话号码校验
    /// </summary>
    public class PhoneAttribute: BaseValidateAttribute
    {
        private string format = null;
        private string remark = null;

        public string GetFormat { get { return this.format; } }

        public string GetRemark { get { return this.remark; } }

        /// <summary>
        /// 电话号码校验
        /// </summary>
        /// <param name="remark"></param>
        /// <param name="format"></param>
        public PhoneAttribute(string remark, string format = @"/(^$)|(^(0\d{2})-(\d{8})$)|(^(0\d{3})-(\d{7,8})$)|(^(0\d{2})-(\d{8})-(\d+)$)|(^(0\d{3})-(\d{7,8})-(\d+)$)/")
        {
            this.format = format;
            this.remark = remark;
        }

        public override bool Validate(object oValue)
        {
            if (oValue != null && !string.IsNullOrEmpty(oValue.ToString()))
            {
                var formats = this.format.Split('=');
                var ispass = true;
                foreach (var item in formats)
                {
                    if (!Regex.IsMatch(oValue.ToString(), @"(" + item + ")"))
                    {
                        ispass = false;
                        break;
                    }
                }
                return ispass;
            }
            else
            {
                return true;
            }
               
        }
    }
}
